#ifndef KNXCONTROLLER_H
#define KNXCONTROLLER_H

#include <QThread>
#include <QHostAddress>
#include <QUdpSocket>
#include <QMutex>
#include <QWaitCondition>
#include "qt_knx.h"
#include "knxengine.h"

/*
 * 1. set up the args
 * 2. start the controller
 * 3. do something
 * 3. stop the controller
 *
    KNXController cntKnx;
    cntKnx.setConenctType(KNX_IPS,QString("192.168.0.222"),setting->KnxIpAddress);

    char v = 0x1;
    cntKnx.doWork();

    int time = 10;
    while(time -- >0)
    {
        cntKnx.writeValue(QByteArray("1/2/11"),ONE_BIT_VALUE,QByteArray(&v));
        sleep(10);
    }

    cntKnx.doStop();
*/

class KNXController : public QThread
{
    Q_OBJECT
public:

    explicit KNXController(QObject *parent = 0);
    ~KNXController();

    bool setConenctType(KNX_CONNECT_TYPE type, QString serverIp, QString localIpAddr);

    void doWork();
    void doStop();

    QString serverIpAddress();

    int busMonitor_data_len;

signals:
    void CBDeviceValueUpdate(void *devObj,QByteArray value,void* userdata);
    void CBServerErrorNotify(QString msg,int errcode);
    void CBRoutingMonitorNotify(QString addr,QByteArray value);

    void KNXEngineReadValue(QByteArray address,int type);
    void KNXEngineWriteValue(QByteArray address,int type,QByteArray value);

    void KNXRegistObject(void *devObj,void *userdata);
    void KNXUnRegistObject(void *devObj,void *userdata);

    void KNXObjUpdate();

public slots:

    void registeEibObject(void *devObj,void *userdata);
    void unRegisterEibObject(void *devObj,void *userdata);

    void writeValue(QByteArray address,int type,QByteArray value);
    void readValue(QByteArray address, int type);
    void readUdpData();

protected:
    void run();

private:

    KNXEngine *_knx;
    QString self;
    QUdpSocket _udpSocket;

    KNX_CONNECT_TYPE type;
    QString serverIp;
    QString localIpAddr;
    quint16 control_port,data_port;

    QWaitCondition threadWait;
    QMutex  mutex;
};

#endif // KNXCONTROLLER_H
